<template>
  <el-container style="border: 1px solid #eee">
    <el-header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item
          v-for="(item, index) in breadList"
          :key="index"
          :to="{ path: item.path}"
          >{{item.meta.title}}</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>
    <el-container>
      <el-aside width="200px" style="height:100%; background-color: rgb(238, 241, 246)">
        <el-menu :router="true">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">主页</span>
          </el-menu-item>
          <el-submenu index="2">
            <template slot="title"><i class="el-icon-picture"></i>图库管理</template>
              <el-menu-item  index="/picManage/picList">图库列表</el-menu-item>
              <el-menu-item  index="/picManage/picCategory">图库分类</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped lang="scss">
  .el-header {
    background-color: #f5f7fa;
    color: #333;
    line-height: 60px;
    .el-breadcrumb{
      line-height: inherit;
    }
  }
  .el-aside {
    color: #333;
  }
</style>

<script>
  export default {
    data() {
      return {
        breadList:[],
        routeName:''
      }
    },
    watch: {
      $route(){
        this.routeName = this.$route.name
        this.getBreadcrumb()
      }
    },
    created(){
      this.routeName = this.$route.name
      this.getBreadcrumb()
    },
    methods: {
      isHome(route){
        if(route) {
          return route.name === "home"
        }       
      },
      getBreadcrumb() {
        let matched = this.$route.matched
        // 如果不是首页,则在数组最前面加上首页
        if(!this.isHome(matched[0])) {
          matched = [{ path: "/home", meta: { title: "首页" }}].concat(matched)
        }
        this.breadList = matched
      }
    }
  };
</script>